﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Logic;
using Entities;

namespace Presentation
{
	public partial class InfoPoster : UserControl
	{
		public InfoPoster()
		{
			InitializeComponent();
		}

		public void Initialize(Movie entity)
		{
			FillData(entity);
			pictureBoxPoster.AllowDrop = true;
		}

		public void FillData(Movie entity)
		{
			Stream stream = File.Open(entity.Poster, FileMode.Open, FileAccess.Read, FileShare.Delete);

			pictureBoxPoster.Image = Image.FromStream(stream);
			pictureBoxPoster.Tag = entity.Poster;

			stream.Close();
		}

		public string GetPoster()
		{
			return pictureBoxPoster.Tag.ToString();
		}

		private void pictureBoxPoster_DragDrop(object sender, DragEventArgs e)
		{
			if (e.Data.GetDataPresent(DataFormats.StringFormat))
			{
				Stream stream = System.Net.WebRequest.Create(e.Data.GetData(DataFormats.StringFormat).ToString()).GetResponse().GetResponseStream();

				pictureBoxPoster.Image = Image.FromStream(stream);
				pictureBoxPoster.Tag = e.Data.GetData(DataFormats.StringFormat).ToString();

				stream.Close();
			}
			else if (e.Data.GetDataPresent(DataFormats.FileDrop))
			{
				string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

				Stream stream = File.Open(files[0], FileMode.Open, FileAccess.Read, FileShare.Delete);

				pictureBoxPoster.Image = Image.FromStream(stream);
				pictureBoxPoster.Tag = files[0];

				stream.Close();
			}
		}

		private void pictureBoxPoster_DragEnter(object sender, DragEventArgs e)
		{
			if (e.Data.GetDataPresent(DataFormats.StringFormat) || e.Data.GetDataPresent(DataFormats.FileDrop))
				e.Effect = DragDropEffects.All;
			else
				e.Effect = DragDropEffects.None;
		}
	}
}
